package com.xywang.servlet.user;

import com.xywang.pojo.User;
import com.xywang.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ServletLogout extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        // 移除用户session中存放的登录状态信息
        // req.getSession().invalidate();
        req.getSession().removeAttribute(Constants.USER_SESSION);
        System.out.println("用户："+user.getUserName()+" 注销成功！");
        // 重定向到登录页面
        resp.sendRedirect(req.getContextPath()+"/login.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
